<template>
	<view class="tab-bar">
		<view v-for="(item, index) in tabs" :key="index" class="tab-item" :class="{ active: currentIndex === index }"
			@click="switchTab(index)">
			<text>{{ item.text }}</text>
		</view>
	</view>
	</view>
</template>

<script>
	import {
		getTihuo,
		getOrder,
		getprice
	} from '@/api/delivery.js';
	import axios from '@/util/http'
	export default {
		data() {
			return {
				currentIndex: 1,
				tabs: [{
						text: '全部'
					},
					{
						text: '未提货'
					},
					{
						text: '已提货'
					},
					// 添加更多 Tab
				],
				items: [

				],
				type: 0,
				req: {
					contactInformation: '',
					beginTime: '',
					endTime: ''
				}
			}
		},
		props: {
			contact: {
				type: String,
				required: true
			},
			selectedOption: {
				type: String,
				required: true
			},
		},
		mounted() {},
		methods: {
			switchTab(index) {
				this.currentIndex = index;
				// if(this.selectedOption==='司机端'){
				//  this.type=0
				//  //全部
				//  if(this.currentIndex==0){
				//  	 getprice({contactInformation: this.contact,type:this.type}).then(res =>{
				//  	 	this.items=res.data.data
				//  		this.$emit('update:data', { currentIndex: this.currentIndex,items: [...this.items]  });
				//  	 }).catch((error) => {
				//  	 });
				//  }
				//  //送货
				//  if(this.currentIndex===1){
				//  		 this.type=1
				//  		getprice({contactInformation: this.contact,type:this.type}).then(res =>{
				//  			this.items=res.data.data
				//  							  		this.$emit('update:data', { currentIndex: this.currentIndex,items: [...this.items]  });
				//  		}).catch((error) => {
				//  		});
				//  }
				//  //网络订单
				//  if(this.currentIndex===2){
				//  		this.type=2
				//  		getprice({contactInformation: this.contact,type:this.type}).then(res =>{
				//  			this.items=res.data.data
				//  							  		this.$emit('update:data', { currentIndex: this.currentIndex,items: [...this.items]  });
				//  		}).catch((error) => {
				//  		});
				//  }
				// }else{
				//全部
				if (this.currentIndex == 0) {
					this.type = 0
					uni.showLoading({
									        title: '加载中'
									  })
					getOrder({
						contactInformation: this.contact
					}).then(res => {
						this.items = res.data.data
						getprice({
							contactInformation: this.contact,
							type: this.type
						}).then(res => {
							let newItem = res.data.data
							newItem.forEach(item => {
								this.items.push(item);
							});
							this.$emit('update:data', {
								currentIndex: this.currentIndex,
								items: [...this.items]
							});
							uni.hideLoading()
						}).catch((error) => {});
					}).catch((error) => {});
					
				}
				//未提货
				if (this.currentIndex === 1) {
					this.type = 0
					const queryParams = {
						contactInformation: this.contact, // 示例参数，根据实际需求调整
						type: this.type, // 示例参数，根据实际需求调整
						// 可以添加更多参数...
					};
					uni.showLoading({
									        title: '加载中'
									  })
					getTihuo(queryParams).then(res => {
						this.items = res.data.data
						getprice({
							contactInformation: this.contact,
							type: 1
						}).then(res => {
							uni.hideLoading()
							let newItem = res.data.data
							newItem.forEach(item => {
								this.items.push(item);
							});
							// getprice({
							// 	contactInformation: this.contact,
							// 	type: 2
							// }).then(res => {
							// 	let newItem = res.data.data
							// 	newItem.forEach(item => {
							// 		this.items.push(item);
							// 	});
								this.$emit('update:data', {
									currentIndex: this.currentIndex,
									items: [...this.items]
								});
							// }).catch((error) => {});
						}).catch((error) => {
							uni.hideLoading()
						});
					}).catch((error) => {
						uni.hideLoading()
						console.error('Error fetching appointment:', error);
					});
				}
				//已提货
				if (this.currentIndex === 2) {
					this.type = 1
					const queryParams = {
						contactInformation: this.contact, // 示例参数，根据实际需求调整
						type: this.type, // 示例参数，根据实际需求调整
						// 可以添加更多参数...
					};
					getTihuo(queryParams).then(res => {
						uni.showLoading({
										        title: '加载中'
										  })
						this.items = res.data.data
						getprice({
							contactInformation: this.contact,
							type: 2
						}).then(res => {
							uni.hideLoading()
							let newItem = res.data.data
							newItem.forEach(item => {
								this.items.push(item);
								})
								uni.hideLoading()
								this.$emit('update:data', {
									currentIndex: this.currentIndex,
									items: [...this.items]
								});
							}).catch((error) => {
								uni.hideLoading()
							});
					}).catch((error) => {
						uni.hideLoading()
						console.error('Error fetching appointment:', error);
					
				});
}
				// 触发自定义事件，并传递数据
				console.log(this.items)

			},
		}
	}
</script>

<style scoped>
	@import "deliveryTabBar.css";
</style>